package com.danceforyou.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.*;

public class AliOssUtil {

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    private static final String endpoint = ConfigureParser.getPropert("OSS_ENDPOINT");
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private static final String accessKeyId = ConfigureParser.getPropert("OSS_ACCESS_KEY_ID");
    private static final String accessKeySecret = ConfigureParser.getPropert("OSS_ACCESS_KEY_SECRET");
    private static final String bucketName = ConfigureParser.getPropert("OSS_BUCKET_NAME");

    public static boolean putObject(String ossKey, byte[] object) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossKey, new ByteArrayInputStream(object));
            // 上传。
            ossClient.putObject(putObjectRequest);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static byte[] getObject(String ossKey) {
        OSS ossClient = null;
        OSSObject ossObject = null;
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
            ossObject = ossClient.getObject(bucketName, ossKey);
            // 读取文件内容。
            inputStream = ossObject.getObjectContent();
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int num = inputStream.read(buffer);
            while (num != -1) {
                outputStream.write(buffer, 0, num);
                num = inputStream.read(buffer);
            }
            outputStream.flush();
            return outputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ossObject != null) {
                try {
                    ossObject.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
